﻿using SoldiHive.Infrustructure.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;

namespace SoldiHive.Model.Views
{
	[Serializable]
	public class RatingsView
	{
		public Company Company { get; set; }
		public double CurrentPrice { get; set; }
		public IList<Rating> Ratings { get; set; }
		public double AvarageRating { get; set; }
		public double AvaragePriceTarget { get; set; }

		public RatingsView(Company company, double currentPrice, IList<Rating> ratings)
		{
			this.Company = company;
			this.CurrentPrice = currentPrice;
			this.Ratings = ratings;

			this.AvarageRating = this.Ratings
				.Where(rating => rating.Rate.HasValue)
				.Select(raiting => raiting.Rate.Value)
				.Average();

			this.AvaragePriceTarget = this.Ratings
				.Where(rating => rating.PriceTarget.HasValue)
				.Select(raiting => raiting.PriceTarget.Value)
				.Average();
		}

		public override string ToString()
		{
			var ratings = this.Ratings.Select(rating => rating.ToString());
			var ratingsString = string.Join(", ", ratings);

			var format = "{0:0.0} [{1}] {2} → {3} {4} {5} {6}";
			var calendarViewString = string.Format(format,
				this.AvarageRating,
				ratingsString,
				string.Format("${0:0.##}", this.CurrentPrice).Align(7),
				string.Format("${0:0.##}", this.AvaragePriceTarget).Align(7),
				string.Format("({0:0.##}%)", (1 - this.CurrentPrice / this.AvaragePriceTarget) * 100).Align(9),
				this.Company.Name,
				this.Company.SubIndustry.Name);

			return calendarViewString;
		}
	}
}
